package com.kongjs.application.controller.rest;

import com.kongjs.application.annotation.OperationLogging;
import com.kongjs.application.convert.TenantsConverter;
import com.kongjs.application.enums.OperationName;
import com.kongjs.application.enums.OperationType;
import com.kongjs.application.model.dto.*;
import com.kongjs.application.model.vo.*;
import com.kongjs.application.service.ITenantService;
import com.kongjs.application.util.PageUtil;
import com.kongjs.common.rest.annotation.Rest;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Rest
@RestController
@RequestMapping("/tenants")
public class TenantsController {
    @Resource
    private TenantsConverter tenantsConverter;
    @Resource
    private ITenantService tenantService;

    @PreAuthorize("hasAuthority('api:tenant:save:one')")
    @PostMapping
    @OperationLogging(operationName = OperationName.TENANT, operationType = OperationType.SAVE_ONE)
    public boolean save(@RequestBody @Validated SaveTenantVO vo) {
        SaveTenantDTO dto = tenantsConverter.toDto(vo);
        return tenantService.save(dto);
    }

    @PreAuthorize("hasAuthority('api:tenant:remove:one')")
    @DeleteMapping("/{id}")
    @OperationLogging(operationName = OperationName.TENANT, operationType = OperationType.REMOVE_ONE)
    public boolean remove(@PathVariable("id") Long id) {
        return tenantService.remove(id);
    }

    @PreAuthorize("hasAuthority('api:tenant:update:one')")
    @PutMapping("/{id}")
    @OperationLogging(operationName = OperationName.TENANT, operationType = OperationType.UPDATE_ONE)
    public boolean update(@PathVariable("id") Long id, @RequestBody @Validated UpdateTenantVO vo) {
        UpdateTenantDTO dto = tenantsConverter.toDto(vo);
        dto.setId(id);
        return tenantService.update(dto);
    }

    @PreAuthorize("hasAuthority('api:tenant:update:one')")
    @PatchMapping("/{id}")
    @OperationLogging(operationName = OperationName.TENANT, operationType = OperationType.UPDATE_ONE)
    public boolean updatePart(@PathVariable("id") Long id, @RequestBody UpdateTenantVO vo) {
        UpdateTenantDTO dto = tenantsConverter.toDto(vo);
        dto.setId(id);
        return tenantService.update(dto);
    }

    @PreAuthorize("hasAuthority('api:tenant:query:one')")
    @GetMapping("/{id}")
    @OperationLogging(operationName = OperationName.TENANT, operationType = OperationType.QUERY_ONE,recordResponse = false)
    public QueryTenantVO query(@PathVariable("id") Long id) {
        QueryTenantDTO dto = tenantService.query(id);
        return tenantsConverter.toVo(dto);
    }

    @PreAuthorize("hasAuthority('api:tenant:query:list')")
    @GetMapping
    @OperationLogging(operationName = OperationName.TENANT, operationType = OperationType.QUERY_LIST, recordResponse = false)
    public List<QueryTenantVO> list() {
        List<QueryTenantDTO> dtos = tenantService.queryList();
        return tenantsConverter.toVos(dtos);
    }

    @PreAuthorize("hasAuthority('api:tenant:query:page')")
    @GetMapping("/page")
    @OperationLogging(operationName = OperationName.TENANT, operationType = OperationType.QUERY_LIST, recordResponse = false)
    public PageVO<QueryTenantVO> page(@Validated PageQueryVO vo, QueryTenantParamVO paramVO) {
        PageQueryDTO dto = new PageQueryDTO();
        dto.setPageNum(vo.getPageNum());
        dto.setPageSize(vo.getPageSize());
        QueryTenantParamDTO paramDTO = tenantsConverter.toDto(paramVO);
        PageDTO<QueryTenantDTO> pageDTO = tenantService.page(dto, paramDTO);
        List<QueryTenantVO> vos = tenantsConverter.toVos(pageDTO.getItems());
        return PageUtil.pageVo(pageDTO, vos);
    }
}
